/***********************************************************************************************************************
    @file    tim1_timebase.c
    @author  FAE Team
    @date    15-Mar-2023
    @brief   THIS FILE PROVIDES ALL THE SYSTEM FUNCTIONS.
  **********************************************************************************************************************
    @attention

    <h2><center>&copy; Copyright(c) <2023> <MindMotion></center></h2>

      Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
    following conditions are met:
    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
       the following disclaimer in the documentation and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or
       promote products derived from this software without specific prior written permission.

      THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *********************************************************************************************************************/

/* Define to prevent recursive inclusion */
#define _TIM1_TIMEBASE_C_

/* Files include */
#include <stdio.h>
#include "platform.h"
#include "timer1.h"
#include "Control.h"
#include "LedDis.h"

/**
 * @addtogroup MM32SPIN0230_LibSamples
 * @{
 */

/**
 * @addtogroup TIM1
 * @{
 */

/**
 * @addtogroup TIM1_TimeBase
 * @{
 */

/* Private typedef ****************************************************************************************************/

/* Private define *****************************************************************************************************/

/* Private macro ******************************************************************************************************/

/* Private variables **************************************************************************************************/
uint32_t u32_systemTime = 0;
// uint32_t u32_1stCompOnWaitTime = 1;       // 第一次上电压机等待时间ms级
uint16_t u16_1stCompOnWaitTime2S = 1; // 第一次上电压机等待转换成秒级
uint16_t u16_CompressorOffTime2S = 0; // 秒单位
// uint32_t u32_CompOnAccumulateTime = 0;   // 压机累计开机时间ms级
uint32_t u32_CompOnAccumulateTime2S = 0; // 秒单位
// uint32_t u32_DefrostTime = 0;            // 除霜时间 ms 单位
uint32_t u32_DefrostTime2S = 0; // 除霜时间 分钟单位

// uint32_t u32_HighTempErrTime = 0;
uint16_t u16_HighTempErrTime2S = 0;
// uint32_t u32_LowTempErrTime = 0;
uint16_t u16_LowTempErrTime2S = 0;
uint16_t u16_SensorErrCompOnOffTime = 0;

/* Private functions **************************************************************************************************/

/***********************************************************************************************************************
 * @brief
 * @note   none
 * @param  none
 * @retval none
 *********************************************************************************************************************/
void TIM1_Configure(void)
{
  NVIC_InitTypeDef NVIC_InitStruct;
  RCC_ClocksTypeDef RCC_Clocks;
  TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;

  RCC_GetClocksFreq(&RCC_Clocks);

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM1, ENABLE);

  TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct);
  TIM_TimeBaseInitStruct.TIM_Prescaler = (RCC_Clocks.PCLK1_Frequency / 10000 - 1); // 10k
  TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInitStruct.TIM_Period = (10 - 1);            // 定时1ms
  TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_Div1; // 10K/2=5000
  TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;
  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStruct);

  TIM_ClearFlag(TIM1, TIM_FLAG_Update);
  TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);

  NVIC_InitStruct.NVIC_IRQChannel = TIM1_BRK_UP_TRG_COM_IRQn;
  NVIC_InitStruct.NVIC_IRQChannelPriority = 0x01;
  NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStruct);

  TIM_Cmd(TIM1, ENABLE);
}

/***********************************************************************************************************************
 * @brief
 * @note   none
 * @param  none
 * @retval none
 *********************************************************************************************************************/
void TIM1_IRQBackCall(void)
{
  u32_systemTime++;

  if (u32_systemTime % ONE_FOURTH == 0)
  {
    u8_Time250msState = 1;
  }
  if (u32_systemTime % HALF_SECOND == 0)
  {
    u8_Time500msState = 1;
  }
  if (u32_systemTime % ONE_SECOND == 0)
  {
    u8_Time1SState = 1;
  }
}

/**
 * @}
 */

/**
 * @}
 */

/**
 * @}
 */

/********************************************** (C) Copyright MindMotion **********************************************/
